package edu.tms.dao;

import java.util.List;
import java.util.Map;

import edu.tms.bean.Picture;
import edu.tms.crud.JdbcUtil;
import edu.tms.dao.common.CommnonDao;

/**
 * 照片存储表dao
 * @author Administrator
 *
 */
public class PictureDaoImp implements CommnonDao<Picture>{
	/**
	 * 保存内容
	 * @param value
	 * @return 
	 * @throws Exception
	 */
	@Override
	public long save(Picture value) throws Exception {
		Picture picture = value;
		String sql="INSERT INTO t_picture (album_id ,picture_name ,picture_url ,create_time )values (?,?,?,?) ";
		long Number=JdbcUtil.insertAuto(sql, picture.getAlbumId(),
				picture.getPictureName(),
				picture.getPictureUrl(),
				picture.getCreateTime());
		return Number;
	}
	/**
	 * 修改内容
	 * @param value
	 * @throws Exception
	 */
	@Override
	public int update(Picture value) throws Exception {
		Picture picture = value;
		String sql="UPDATE t_picture SET album_id =?,picture_name =?,picture_url =? WHERE picture_name =? OR picture_id=? ";
		int Number= JdbcUtil.update(sql,picture.getAlbumId(),
				picture.getPictureName(),
				picture.getPictureUrl(),
				picture.getPictureName(),
				picture.getPictureId());
		return Number;
	}
	
	/**
	 * 删除内容
	 * 相册表t_album
	 * @param value
	 * @throws Exception
	 */
	@Override
	public int delete(Picture value) throws Exception {
		return 0;
	}
	
	/**
	 * 查找所有的内容
	 * 相册表t_album
	 * @param pageNo 第几页
	 * @param pageSize 每页数字
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Picture> findAll(int pageNo, int pageSize) throws Exception {
		String sql="SELECT tam.album_name, tam.album_id,tp.picture_name ,tp.picture_url,tp.create_time,tp.picture_state  ";
		sql+="FROM  t_album tam,t_picture tp ";
		sql+="WHERE tam.album_id=tp.album_id  limit ?,?";
		return JdbcUtil.query(Picture.class, sql,(pageNo-1)*pageSize,pageSize);
	}

	/**
	 * String 对应  数据库 列名（comment)
	 * Object 对应相应列名值
	 * 返回的是所查找的类中的数据。方便EXCEL导出
	 */
	@Override
	public List<Map<String,Object>> getMapList() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<Picture> findAll(Object key, int pageNo, int pageSize) throws Exception {
		// TODO Auto-generated method stub
		String sql="SELECT  tam.album_name,tam.album_id,tp.picture_name ,tp.picture_url,tp.create_time,tp.picture_state  ";
		sql+="FROM  t_album tam,t_picture tp ";
		sql+="WHERE tam.album_id=tp.album_id AND tam.album_id=? OR tam.album_name=? or tp.picture_name =?  limit ?,?";
		return JdbcUtil.query(Picture.class, sql,key,key,key,(pageNo-1)*pageSize,pageSize);
	}
	@Override
	public Picture findByKey(Object key) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public List<Picture> findUrl(Object key) throws Exception {
		// TODO Auto-generated method stub
		String sql="SELECT * FROM t_picture WHERE album_id=? limit ?,?";
		return JdbcUtil.query(Picture.class, sql,key,0,4);
	}
}
